<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品表id" prop="basicId">
        <el-input
          v-model="queryParams.basicId"
          placeholder="请输入商品表id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规格id" prop="specificationId">
        <el-input
          v-model="queryParams.specificationId"
          placeholder="请输入规格id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户头像" prop="nameInco">
        <el-input
          v-model="queryParams.nameInco"
          placeholder="请输入用户头像"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户名称" prop="reviewName">
        <el-input
          v-model="queryParams.reviewName"
          placeholder="请输入用户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="评价文字" prop="written">
        <el-input
          v-model="queryParams.written"
          placeholder="请输入评价文字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品分数" prop="shopScore">
        <el-input
          v-model="queryParams.shopScore"
          placeholder="请输入商品分数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="服务分数" prop="serviceScore">
        <el-input
          v-model="queryParams.serviceScore"
          placeholder="请输入服务分数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="评价图片" prop="reviewInco">
        <el-input
          v-model="queryParams.reviewInco"
          placeholder="请输入评价图片"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="评价时间" prop="reviewDate">
        <el-date-picker clearable
                        v-model="queryParams.reviewDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择评价时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['proSystem:review:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['proSystem:review:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['proSystem:review:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['proSystem:review:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="reviewList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="商品名称" align="center" prop="basicName" />
      <el-table-column label="规格名称" align="center" prop="specificationName" />
      <el-table-column label="用户头像" align="center" prop="nameInco" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.nameInco" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="用户名称" align="center" prop="reviewName" />
      <el-table-column label="评价文字" align="center" prop="written" />
      <el-table-column label="商品分数" align="center" prop="shopScore" width="200">
        <template slot-scope="scope">
          <el-rate
            v-model="scope.row.shopScore"
            disabled
            show-score
            text-color="#ff9900"
            score-template="{value}">
          </el-rate>
        </template>
      </el-table-column>
<!--      <el-table-column label="商品分数" align="center" prop="shopScore" />-->
      <el-table-column label="服务分数" align="center" prop="serviceScore" width="200">
        <template slot-scope="scope">
          <el-rate
            v-model="scope.row.serviceScore"
            disabled
            show-score
            text-color="#ff9900"
            score-template="{value}">
          </el-rate>
        </template>
      </el-table-column>
<!--      <el-table-column label="服务分数" align="center" prop="serviceScore" />-->
      <el-table-column label="评价图片" align="center" prop="reviewInco" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.reviewInco" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="评价时间" align="center" prop="reviewDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.reviewDate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>




      <el-table-column label="审核状态" align="center" prop="reviewStatus" width="180">
        <template slot-scope="scope">
          <el-button>{{scope.row.reviewStatus===0?"未审核":"已审核"}}</el-button>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="huifu(scope.row)"
            v-hasPermi="['proSystem:review:edit']"
          >回复</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['proSystem:review:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户评论对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">


        <el-form-item label="商品" prop="basicId">
          <div @click="toReview(form.basicId)"  type="primary">
<!--            <i class="el-icon-goods"></i>-->
              <image-preview :src="this.formId.img" :width="50" :height="50"/>
          </div>
        </el-form-item>

        <el-form-item label="商品规格" prop="specificationId" :hidden="hideGuige">
          <div @click="openGuigeYemian()"  type="primary">
                        <i class="el-icon-goods"></i>
            <i width="50ox">{{resReslt.name}}</i>
          </div>
        </el-form-item>



        <el-form-item  label="用户头像" prop="nameInco">
          <image-upload
            v-model="form.nameInco"
          />

        </el-form-item>

        <el-form-item label="用户名称" prop="reviewName">
          <el-input v-model="form.reviewName" placeholder="请输入用户名称" />
        </el-form-item>
        <el-form-item label="评价文字" prop="written">
          <el-input v-model="form.written" placeholder="请输入评价文字" />
        </el-form-item>
        <el-form-item label="商品分数" prop="shopScore">
          <div class="block">
            <el-rate v-model="form.shopScore"></el-rate>
          </div>
        </el-form-item>



        <el-form-item label="服务分数" prop="serviceScore">
          <div class="block">
            <el-rate v-model="form.serviceScore"></el-rate>
          </div>
        </el-form-item>
        <el-form-item label="评价图片" prop="reviewInco">
          <image-upload v-model="form.reviewInco"/>
        </el-form-item>
        <el-form-item label="评价时间" prop="reviewDate">
          <el-date-picker clearable
                          v-model="form.reviewDate"
                          type="date"
                          value-format="yyyy-MM-dd hh:mm:ss"
                          placeholder="请选择评价时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>




















    <!-- 选择商品界面二级弹出框 -->
    <el-dialog title="选择商品" :visible.sync="openSecond" width="800px" append-to-body>
      <el-form ref="proForm" :model="proForm" :rules="rules" label-width="80px">
        <el-form-item label="商品分类" prop="typeId">
          <div class="block">
            <el-cascader
              :options="checkItem"
              :props="{ checkStrictly: true , value:'id' , label:'name'}"
              :value='checkItem.id'
              :label="checkItem.name"
              clearable></el-cascader>
          </div>
        </el-form-item>
        <el-form-item label="商品搜索" prop="name">
          <el-input v-model="proForm.name"/>        </el-form-item>
      </el-form>


      <el-table
        :data="showItemList"
        height="250"
        border
        style="width: 100%">
        <el-table-column prop="pid" width="30">
          <template slot-scope="scope">
              <el-radio :label="scope.row.id"  @input="checkPro(scope.row)"></el-radio>
          </template>
        </el-table-column>
        <el-table-column
          prop="id"
          label="商品id"
          width="180">
        </el-table-column>
        <el-table-column
          prop="shopName"
          label="商品名称"
          width="280">
        </el-table-column>
        <el-table-column label="图片" align="center" prop="carousel" width="100">
          <template slot-scope="scope">
            <image-preview :src="scope.row.carousel" :width="50" :height="50"/>
          </template>
        </el-table-column>
      </el-table>
<!--      <div slot="footer" class="dialog-footer">-->
<!--        <el-button type="primary" @click="submitPro">确 定</el-button>-->
<!--        <el-button @click="cancelPro">取 消</el-button>-->
<!--      </div>-->
      <pagination
        v-show="prototal>0"
        :total="prototal"
        :page.sync="showItemList.pageNum"
        :limit.sync="showItemList.pageSize"
        @pagination="findAllItem"
      />
    </el-dialog>




<!--回复弹窗添加-->
    <el-dialog title="添加回复" :visible.sync="openHuifu" width="500px" append-to-body>
      <el-form ref="replyForm" :model="replyForm" :rules="rules" label-width="80px">
        <el-form-item label="回复内容" prop="reply">
          <el-input v-model="replyForm.reply" placeholder="请输入评价" />
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitReply()">确 定</el-button>
        <el-button @click="cancelReply">取 消</el-button>
      </div>
    </el-dialog>




<!--    规格添加界面弹窗-->
    <el-dialog title="规格" :visible.sync="hideGuigeYemian" width="500px" append-to-body>
      <el-table
        :data="showGuigeList"
        height="250"
        border
        style="width: 100%">
        <el-table-column prop="pid" width="30">
          <template slot-scope="scope">
            <el-radio :label="scope.row.id"  @input="checkRes(scope.row)"></el-radio>
          </template>
        </el-table-column>
        <el-table-column
          prop="id"
          label="规格id"
          width="180">
        </el-table-column>
        <el-table-column
          prop="name"
          label="规格每次"
          width="280">
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>

</template>

<script>
import {
  listReview,
  getReview,
  delReview,
  addReview,
  updateReview,
  toRe,
  listSpecification,
  addReply
} from "@/api/product/shopReview";
import { jssType , listPro } from "@/api/system/pro";
import {listItem} from "@/api/product/pro";


export default {
  name: "Review",
  data() {
    return {
      huifuModel:{},
      resReslt:{},
      showGuigeList:[],
      hideGuigeYemian:false,
      hideGuige:true,
      formId:{},
      // b绑定的model
      replyForm:{},
      openHuifu:false,
      showItemList:[],
      checkModel:{},
      checkItem:[],
      itemList:{},
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedSysShopReply: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      prototal:0,
      // 用户评论表格数据
      reviewList: [],
      // ${subTable.functionName}表格数据
      sysShopReplyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openSecond:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        basicId: null,
        specificationId: null,
        nameInco: null,
        reviewName: null,
        written: null,
        shopScore: null,
        serviceScore: null,
        reviewInco: null,
        reviewDate: null,
        replyStatus: null,
        reviewStatus: null
      },
      // 表单参数
      form: {},
      proForm:{},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.findGuige();
    this.findAllItem();
    this.getItem();
    this.getList();
  },
  methods: {
    checkRes(row){
      this.resReslt.id=row.id;
      this.resReslt.name=row.name;
      this.hideGuigeYemian=false;
    },
    findGuige(){
      listSpecification().then(response => {
        this.showGuigeList=response.rows;
      })

    },
    openGuigeYemian(){
      this.hideGuigeYemian=true;
    },
    cancelReply(){
      this.replyForm={};
      this.openHuifu=false;
    },
    submitReply(){
        this.huifuModel.reply=this.replyForm.reply;
        console.log(this.huifuModel+"asdasdas")
      addReply(this.huifuModel).then(response => {
        this.openHuifu=false;
        this.huifuModel={
          reviewId:null,
          reply:null,
        };
        this.replyForm={};
        this.getList();
      })
    },
    huifu(row){
      this.huifuModel.reviewId=row.id;
      this.openHuifu=true;
    },
    checkPro(row){
      this.formId.id=row.id;
      this.formId.img=row.carousel;
      console.log(this.formId)
      this.openSecond=false;
      if(this.formId!=null){
        this.hideGuige=false;
      }
    },
    findAllItem(){
      listItem(this.proForm).then(response => {
        this.prototal=response.total;
        this.showItemList=response.rows;
      })
    },
    getItem(){
      listPro().then(response => {
        this.checkItem=response.rows;
      })
    },
    toReview(id){
      this.openSecond=true;
    },
    /** 查询用户评论列表 */
    getList() {
      this.loading = true;
      listReview(this.queryParams).then(response => {
        this.reviewList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    cancelPro() {
      this.openSecond = false;
      this.proForm=null;
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        basicId: null,
        specificationId: null,
        nameInco: null,
        reviewName: null,
        written: null,
        shopScore: null,
        serviceScore: null,
        reviewInco: null,
        reviewDate: null,
        replyStatus: null,
        reviewStatus: null
      };
      this.sysShopReplyList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户评论";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getReview(id).then(response => {
        this.form = response.data;
        this.sysShopReplyList = response.data.sysShopReplyList;
        this.open = true;
        this.title = "修改用户评论";
      });
    },
    submitPro(){

    },

    /** 提交按钮 */
    submitForm() {
      this.form.basicId=this.formId.id;
      this.form.specificationId=this.resReslt.id;
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.sysShopReplyList = this.sysShopReplyList;
          if (this.form.id != null) {
            updateReview(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.formId={};
              this.resReslt={},
              this.getList();
            });
          } else {
            addReview(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.formId={};
              this.resReslt={},
                this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除用户评论编号为"' + ids + '"的数据项？').then(function () {
        return delReview(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** ${subTable.functionName}序号 */
    rowSysShopReplyIndex({row, rowIndex}) {
      row.index = rowIndex + 1;
    },
    /** ${subTable.functionName}添加按钮操作 */
    handleAddSysShopReply() {
      let obj = {};
      obj.reply = "";
      obj.pid = "";
      this.sysShopReplyList.push(obj);
    },
    /** ${subTable.functionName}删除按钮操作 */
    handleDeleteSysShopReply() {
      if (this.checkedSysShopReply.length == 0) {
        this.$modal.msgError("请先选择要删除的${subTable.functionName}数据");
      } else {
        const sysShopReplyList = this.sysShopReplyList;
        const checkedSysShopReply = this.checkedSysShopReply;
        this.sysShopReplyList = sysShopReplyList.filter(function (item) {
          return checkedSysShopReply.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleSysShopReplySelectionChange(selection) {
      this.checkedSysShopReply = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('proSystem/review/export', {
        ...this.queryParams
      }, `review_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style>
.el-icon-goods {
  line-height: 40px;
  position: relative;
  font-size: 80px;
}
</style>
